﻿
namespace SwtorCalculator.Talent {
    using System;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using Talents;

    /// <summary>
    /// Interaction logic for TalentButton.xaml
    /// </summary>
    public partial class TalentButton : UserControl {
        private ImageSource ABILITY = new BitmapImage( new Uri( @"pack://application:,,,/SwtorCalculator;component/Images/TalentCalc/TalentBtnGreen2.png", UriKind.Absolute ) );
        private ImageSource ABILITY_MAX = new BitmapImage( new Uri( @"pack://application:,,,/SwtorCalculator;component/Images/TalentCalc/TalentBtnGreenGold2.png", UriKind.Absolute ) );
        private ImageSource DISABLED = new BitmapImage( new Uri( @"pack://application:,,,/SwtorCalculator;component/Images/TalentCalc/TalentBtnGray2.png", UriKind.Absolute ) );
        private ImageSource NORMAL = new BitmapImage( new Uri( @"pack://application:,,,/SwtorCalculator;component/Images/TalentCalc/TalentBtnBlue2.png", UriKind.Absolute ) );
        private ImageSource NORMAL_MAX = new BitmapImage( new Uri( @"pack://application:,,,/SwtorCalculator;component/Images/TalentCalc/TalentBtnBlueGold2.png", UriKind.Absolute ) );
        private const string ICON_PREFIX = @"pack://application:,,,/SwtorCalculator;component/Images/TalentIcons/";

        /// <summary>
        /// TalentButton no-arg constructor
        /// </summary>
        public TalentButton() {
            InitializeComponent();
        }

        internal TalentPanel TalentPanel { get { return ( TalentPanel )( ( ( Grid )Parent ).Parent ); } }

        private TalentDataAttribute talentData;
        internal TalentDataAttribute TalentData {
            get { return talentData; }
            set {
                talentData = value;
                current = talentData == null ? 0 : ( int )Math.Min( TalentPanel.CharacterTalents.PointsSpent[talentData.Index], talentData.MaxPoints );
            }
        }

        private int current;
        internal int Current {
            get { return current; }
            set {
                if( talentData != null && value >= 0 && value <= talentData.MaxPoints && CanPutPoints() ) {
                    if( value < current )
                        TalentTab.PtsAvailable++;
                    else
                        TalentTab.PtsAvailable--;
                    current = value;
                    TalentPanel.CharacterTalents.PointsSpent[TalentData.Index] = current;
                    TalentPanel.RankChanged();
                    
                }
            }
        }

        internal bool IsMaxRank() { return current == talentData.MaxPoints; }

        internal bool IsBelowAboveMax( bool isDecrement ) {
            if( isDecrement )
                return ( TalentTab.PtsAvailable - 1 ) >= 0;
            else
                return ( TalentTab.PtsAvailable + 1 ) <= 41;
        }

        internal bool CanPutPoints() {
            return TalentPanel.PointsBelowRow( talentData.Row ) >= ( talentData.Row - 1 ) * 5 &&
                ( talentData.Prerquisite < 0 ||
                TalentPanel.CharacterTalents.PointsSpent[talentData.Prerquisite] == TalentPanel.GetAttribute( talentData.Prerquisite ).MaxPoints )
                /*&& TalentTab.PtsAvailable > 0*/;
        }

        internal void Update() {
            if( talentData != null ) {
                if( Current == talentData.MaxPoints ) {
                    if( talentData.IsAbility )
                        OverlayImg.Source = ABILITY_MAX;
                    else
                        OverlayImg.Source = NORMAL_MAX;

                    RankTxt.Foreground = new SolidColorBrush( Colors.Black );
                    RankTxt.Text = string.Format( "{0}", talentData.MaxPoints );
                    FadeRec.Visibility = Visibility.Collapsed;
                }

                else {
                    if( CanPutPoints() && ( TalentTab.PtsAvailable > 0 || Current > 0 ) ) {
                        if( talentData.IsAbility )
                            OverlayImg.Source = ABILITY;
                        else
                            OverlayImg.Source = NORMAL;

                        FadeRec.Visibility = Visibility.Collapsed;
                    }

                    else {
                        OverlayImg.Source = DISABLED;
                        FadeRec.Visibility = Visibility.Visible;
                    }

                    RankTxt.Foreground = new SolidColorBrush( Colors.White );
                    RankTxt.Text = string.Format( "{0} {1}", Current, talentData.MaxPoints );
                }

                TalentImg.Source = new BitmapImage( new Uri( ICON_PREFIX + talentData.Icon, UriKind.Absolute ) );

                ToolTipService.SetToolTip( this, GetTooltipString() );
                this.Visibility = Visibility.Visible;
            }

            else {
                this.Visibility = Visibility.Collapsed;
            }
        }

        public string GetTooltipString() {
            string n = talentData.Name + "\n\n";

            if( Current == 0 ) {
                return string.Format( n + "Next Rank:\n{0}", General.wrapText( talentData.Description[0] ) );
            }

            else if( Current == talentData.MaxPoints ) {
                return string.Format( n + "{0}", General.wrapText( talentData.Description[talentData.MaxPoints - 1] ) );
            }

            else {
                return string.Format( n + "{0}\n\nNext Rank:\n{1}", General.wrapText( talentData.Description[Current - 1] ), General.wrapText( talentData.Description[Current] ) );
            }
        }

        private void TalentClicked( object sender, System.Windows.Input.MouseButtonEventArgs e ) {
            if( e.ChangedButton == MouseButton.Left ) {
                if( TalentTab.PtsAvailable > 0 )
                    Current++;
            }

            else {
                if( TalentTab.PtsAvailable < 41 )
                    Current--;
            }
        }
    }
}
